package com.cqc.supportlibary01;

import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.lang.reflect.Field;

/**
 * 1 找到带有behavior_sheet 的控件
 * 2 获取behavior对象
 * 3 设置回调
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private NestedScrollView nestedScrollView;
    private BottomSheetBehavior<NestedScrollView> behavior;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn_confirm;
    private Button btn_confirm2;
    private BottomSheetDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();
    }


    private void initViews() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);

        //        behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                // newState:1  2  3  4 5
                LogUtil.d(TAG, "newState=" + newState);
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                //slideOffset: (0,1]
                //LogUtil.d(TAG,"slideOffset="+slideOffset);
            }
        });
    }

    private void findViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

        behavior = BottomSheetBehavior.from(nestedScrollView);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                int state = behavior.getState();
                if (state == BottomSheetBehavior.STATE_EXPANDED) {
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                } else {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
                break;
            case R.id.btn2:
                dialog = new BottomSheetDialog(MainActivity.this);

                //第一种：setContentView(resId)
                dialog.setContentView(R.layout.dialog_bottom_sheet);
                btn_confirm2 = (Button) dialog.findViewById(R.id.btn_confirm2);
                if (btn_confirm2 != null) {
                    btn_confirm2.setOnClickListener(this);
                }

                //第二种：setContentView(view)
                //                View view = getLayoutInflater().inflate(R.layout.dialog_bottom_sheet, null);
                //                btn_confirm2 = (Button) view.findViewById(R.id.btn_confirm2);
                //                btn_confirm2.setOnClickListener(this);
                //                dialog.setContentView(view);

                dialog.show();
                break;

            case R.id.btn3:
                BottomSheetDialogFragment1 dialogFrag = new BottomSheetDialogFragment1();
                dialogFrag.show(getSupportFragmentManager(), "BottomSheetDialogFragment1");
                break;
            case R.id.btn4:
                BottomSheetDialogFragment2 frag2 = new BottomSheetDialogFragment2();
                frag2.show(getSupportFragmentManager(), "frag2");
                break;
            case R.id.btn5:
                BottomSheetDialogFragment3 frag3 = new BottomSheetDialogFragment3();
                frag3.show(getSupportFragmentManager(), "frag3");
                break;
            case R.id.btn_confirm://无效
                int state1 = behavior.getState();
                LogUtil.d(TAG, "state1=" + state1);
                behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                break;
            case R.id.btn_confirm2:
                dialog.dismiss();
                break;
        }
    }
}
